package no.saiboten.milkywave.entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.Embedded;
import javax.persistence.Id;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class Event implements Comparable<Event> {
	@Id
	private Long id;

	@Embedded
	private List<User> users;

	private List<String> keyToUserMap;

	public List<String> getKeyToUserMap() {
		return keyToUserMap;
	}

	private Date date;

	public Event() {
		this.users = new ArrayList<User>();
		keyToUserMap = new ArrayList<String>();
	}

	public void generateKeyMap() {
		Objectify ofy = ObjectifyService.begin();
		List<User> users = ofy.query(User.class).list();

		for (User user : users) {
			UUID uuid = UUID.randomUUID();
			String uniqueUuid = uuid.toString();
			keyToUserMap.add(uniqueUuid + "=" + user.getMail());
		}
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDate() {
		return date;
	}

	public String getNiceDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		return sdf.format(date);
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public List<User> getUsers() {
		return users;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public int compareTo(Event o) {
		if (this.getDate().after(o.getDate())) {
			return -1;
		} else if (this.getDate().before(o.getDate())) {
			return 1;
		}
		return 0;
	}

}
